import { get, post } from './request'

// const vueBaseURL = 'http://101.200.220.56:8080'
// const vueBaseURL = 'http://localhost:8080'
const vueBaseURL = ''
const request = { get, post }

export default (apiConfs, prefix = '', host) => {
  const apis = {}
  host = `${vueBaseURL}${prefix}`
  for (const key in apiConfs) {
    let apiConf = apiConfs[key]
    if (!Array.isArray(apiConf)) {
      apiConf = [apiConf]
    }
    apis[key] = {}
    apiConf.forEach((conf) => {
      if (typeof conf === 'string') {
        conf = {
          method: 'get',
          url: conf.indexOf('http') === -1 ? `${host}${conf}` : conf,
        }
      } else {
        conf = {
          method: conf.type || 'get',
          url:
            conf.url.indexOf('http') === -1 ? `${host}${conf.url}` : conf.url,
          params: {
            headers: {
              'Content-type': conf.contentType || 'application/json',
              ...conf.headers,
            },
            ...conf.others,
          },
        }
      }
      const { method, url, params } = conf
      apis[key][method] = (data) => request[method](url, data, params)
      apis[key].url = url
    })
  }
  return apis
}
